/**
 * <p>Title: UserDetailsService.java</p>
 * <p>Description: 系统用户信息校验，权限检查</p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: 深圳市中钞信达成都研发中心</p>
 *
 * @author noel
 * @date 2019年9月6日
 */
package com.song.config;

import com.song.dao.IUser;
import com.song.pojo.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户权限检查
 *
 * @author noel
 * @date 2019年9月6日
 */
@Service(value = "CustomUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    private static final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);

    @Resource
    private IUser iUser;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        登录的主要逻辑    大多数逻辑都交给了Spring security
        // TODO Auto-generated method stub
        System.out.println("username = " + username);
        UserModel userModel = iUser.getUser(username);
        System.out.println("userModel = ******" + userModel);
        if (null == userModel) {
            throw new UsernameNotFoundException("用户不存在");

        }
//        使用自定义的UserDetail
        CustomUserDetails user = new CustomUserDetails(userModel.getId(), userModel.getName(), userModel.getPwd(), userModel.getNickname(), userModel.getFace(), userModel.getType());

        return user;
    }

}
